package com.legal.app.service.impl;

import java.util.Date;
import java.util.List;

import com.legal.app.domain.Order;
import com.legal.app.domain.vo.LawyerCommentVO;
import com.legal.app.mapper.OrderMapper;
import com.legal.common.core.domain.AjaxResult;
import com.legal.common.utils.DateUtils;
import com.legal.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.legal.app.mapper.LawyerCommentMapper;
import com.legal.app.domain.LawyerComment;
import com.legal.app.service.ILawyerCommentService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 律师评论Service业务层处理
 * 
 * @author Ale
 * @date 2025-05-30
 */
@Service
public class LawyerCommentServiceImpl implements ILawyerCommentService 
{
    @Autowired
    private LawyerCommentMapper lawyerCommentMapper;

    /**
     * 查询律师评论
     * 
     * @param id 律师评论主键
     * @return 律师评论
     */
    @Override
    public LawyerComment selectLawyerCommentById(Long id)
    {
        return lawyerCommentMapper.selectLawyerCommentById(id);
    }

    /**
     * 查询律师评论列表
     * 
     * @param lawyerComment 律师评论
     * @return 律师评论
     */
    @Override
    public List<LawyerComment> selectLawyerCommentList(LawyerComment lawyerComment)
    {
        return lawyerCommentMapper.selectLawyerCommentList(lawyerComment);
    }

    /**
     * 新增律师评论
     * 
     * @param lawyerComment 律师评论
     * @return 结果
     */
    @Override
    public int insertLawyerComment(LawyerComment lawyerComment)
    {
        lawyerComment.setCreateTime(DateUtils.getNowDate());
        return lawyerCommentMapper.insertLawyerComment(lawyerComment);
    }

    /**
     * 修改律师评论
     * 
     * @param lawyerComment 律师评论
     * @return 结果
     */
    @Override
    public int updateLawyerComment(LawyerComment lawyerComment)
    {
        return lawyerCommentMapper.updateLawyerComment(lawyerComment);
    }

    /**
     * 批量删除律师评论
     * 
     * @param ids 需要删除的律师评论主键
     * @return 结果
     */
    @Override
    public int deleteLawyerCommentByIds(Long[] ids)
    {
        return lawyerCommentMapper.deleteLawyerCommentByIds(ids);
    }

    /**
     * 删除律师评论信息
     * 
     * @param id 律师评论主键
     * @return 结果
     */
    @Override
    public int deleteLawyerCommentById(Long id)
    {
        return lawyerCommentMapper.deleteLawyerCommentById(id);
    }

    @Override
    public List<LawyerCommentVO> selectLawyerCommentVOList(LawyerComment lawyerComment) {
        return lawyerCommentMapper.selectLawyerCommentVOList(lawyerComment);
    }
    @Override
    @Transactional
    public AjaxResult replyComment(Long id, String content) {
        LawyerComment comment = lawyerCommentMapper.selectLawyerCommentById(id);
        if (comment == null) {
            return AjaxResult.error("评论不存在");
        }
        comment.setReplyContent(content);
        comment.setReplyTime(DateUtils.getNowDate());

        int result = lawyerCommentMapper.updateLawyerComment(comment);
        return result > 0 ? AjaxResult.success("回复成功") : AjaxResult.error("回复失败");
    }

    @Autowired
    private OrderMapper orderMapper;
    @Override
    public AjaxResult addCommentByOrder(Long orderId, String content) {
        // 1. 验证订单存在
        Order order = orderMapper.selectOrderById(orderId);
        if (order == null) {
            return AjaxResult.error("订单不存在");
        }

        // 2. 验证订单状态为已完成（4=已完成）
        if (order.getStatus() != 4) {
            return AjaxResult.error("订单未完成，无法评论");
        }

        // 3. 验证当前用户为订单用户
        Long currentUserId = SecurityUtils.getUserId();
        if (!order.getUserId().equals(currentUserId)) {
            return AjaxResult.error("您没有权限评论此订单");
        }

        // 5. 构建评论对象
        LawyerComment comment = new LawyerComment();
        comment.setUserId(currentUserId);
        comment.setLawyerId(order.getLawyerId());
        comment.setContent(content);
        comment.setStatus(1L);
        // 正常状态
        comment.setCreateTime(new Date());

        // 6. 插入评论
        int result = lawyerCommentMapper.insertLawyerComment(comment);
        if (result != 1) {
            return AjaxResult.error("评论发表失败");
        }
        return AjaxResult.success("评论发表成功");
    }

}
